﻿// Type: GsmComm.PduConverter.SmartMessaging.SmartMessageDecoder
// Assembly: PDUConverter, Version=1.21.0.0, Culture=neutral, PublicKeyToken=515d87df384dcc81

using System.Diagnostics;
using System.Linq;
using GsmComm.PduConverter;
using System;
using System.Collections.Generic;
using System.Text;
using GsmComm.PduConverter.SmartMessaging;
using Lap.Collections;

namespace PpSmsSender
{
    public class MySmartMessageDecoder
    {


        private static List<object> GetConcatUserData(IList<SmsPdu> parts, bool outputAsText, bool allowMissingParts, bool noOutput, out bool allPartsAvailable)
        {
            if (parts == null)
                throw new ArgumentNullException("parts");
            allPartsAvailable = true;
            List<object> list = new List<object>();
            if (parts.Count > 0)
            {
                SmsPdu pdu = parts[0];
                if (GsmComm.PduConverter.SmartMessaging.SmartMessageDecoder.IsPartOfConcatMessage(pdu))
                {
                    SortedList<IConcatenationInfo, SmsPdu> sortedList = SortConcatMessageParts(parts);
                    int num = (int)sortedList.Keys[0].TotalMessages;
                    int index1 = 0;
                    for (int index2 = 1; index2 <= num; ++index2)
                    {
                        bool flag = false;
                        if (index1 >= sortedList.Count)
                        {
                            flag = true;
                        }
                        else
                        {
                            IConcatenationInfo concatenationInfo = sortedList.Keys[index1];
                            SmsPdu smsPdu = sortedList.Values[index1];
                            if (index2 != (int)concatenationInfo.CurrentNumber)
                            {
                                flag = true;
                            }
                            else
                            {
                                if (!noOutput)
                                {
                                    if (outputAsText)
                                        list.Add((object)GetUserDataWithoutHeaderText(smsPdu));
                                    else
                                        list.Add((object)smsPdu.GetUserDataWithoutHeader());
                                }
                                ++index1;
                            }
                        }
                        if (flag)
                        {
                            allPartsAvailable = false;
                            if (!allowMissingParts)
                                throw new ArgumentException("Not all parts of the message are available. Part #" + (object)index2 + " is missing.", "parts");
                            if (!noOutput)
                                list.Add((object)null);
                        }
                    }
                }
                else if (!noOutput)
                {
                    if (outputAsText)
                        list.Add((object)pdu.UserDataText);
                    else
                        list.Add((object)pdu.UserData);
                }
            }
            return list;
        }


        public static string CombineConcatMessageText(IList<SmsPdu> parts)
        {
            bool allPartsAvailable;
            List<object> concatUserData = GetConcatUserData(parts, true, false, false, out allPartsAvailable);
            StringBuilder stringBuilder = new StringBuilder();
            foreach (string str in concatUserData)
                stringBuilder.Append(str);
            return ((object)stringBuilder).ToString();
        }

        private static SortedList<IConcatenationInfo, SmsPdu> SortConcatMessageParts(IList<SmsPdu> parts)
        {
            IConcatenationInfo concatenationInfo1 = (IConcatenationInfo)null;
            SortedList<IConcatenationInfo, SmsPdu> sortedList = new SortedList<IConcatenationInfo, SmsPdu>((IComparer<IConcatenationInfo>)new ConcatInfoComparer());
            foreach (SmsPdu pdu in (IEnumerable<SmsPdu>)parts)
            {
                IConcatenationInfo concatenationInfo2;
                if (concatenationInfo1 == null)
                {
                    concatenationInfo2 = GsmComm.PduConverter.SmartMessaging.SmartMessageDecoder.GetConcatenationInfo(pdu);
                }
                else
                {
                    if (!GsmComm.PduConverter.SmartMessaging.SmartMessageDecoder.IsPartOfConcatMessage(pdu))
                        throw new ArgumentException("A non-concatenated message part is present at an invalid position.", "parts");
                    concatenationInfo2 = GsmComm.PduConverter.SmartMessaging.SmartMessageDecoder.GetConcatenationInfo(pdu);
                    if ((int)concatenationInfo1.ReferenceNumber != (int)concatenationInfo2.ReferenceNumber)
                        throw new ArgumentException("The reference numbers differ between the message parts.", "parts");
                    if ((int)concatenationInfo1.TotalMessages != (int)concatenationInfo2.TotalMessages)
                        throw new ArgumentException("The number of total messages differs between the message parts.", "parts");
                }
                sortedList.Add(concatenationInfo2, pdu);
                concatenationInfo1 = concatenationInfo2;
            }
            return sortedList;
        }

        public static string GetUserDataWithoutHeaderText(SmsPdu pdu)
        {
            if (DataCodingScheme.Decode(pdu.DataCodingScheme).Alphabet == 0)
            {
                //GSMComm isn't handling this conversion correctly
                byte numbytes = (byte) (1+((uint)pdu.UserData[0] + 1U)*8/7);
                pdu.UserData.ForEach(b=>Debug.Write(b+", "));
                Debug.WriteLine((""));
                string userDataWithoutHeaderText = PduParts.DecodeText(pdu.UserData, pdu.DataCodingScheme);
                userDataWithoutHeaderText = userDataWithoutHeaderText.Remove(0, ((pdu.UserData[0] + 1) * 8 + ((pdu.UserData[0] + 1) * 8) % 7) / 7);
                return userDataWithoutHeaderText;

            }
            return pdu.GetUserDataTextWithoutHeader();
        }

    }
}
